import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from 'views/Home.vue';

import Recommend from 'views/recommend/Recommend.vue';

Vue.use(VueRouter);

//解决编程式路由往同一地址跳转时会报错的情况
const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;
//push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
};
//replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalReplace.call(this, location, onResolve, onReject);
  return originalReplace.call(this, location).catch((err) => err);
};

const routes = [
  {
    path: '',
    redirect: '/home',
  },
  {
    path: '/home',
    component: Home,
    children: [
      {
        path: '/',
        name: 'Recommend',
        component: Recommend,
      },
      {
        path: '/playlists',
        name: 'PlayLists',
        component: () => import('views/playlists/PlayLists'),
      },
      {
        path: '/playlist',
        name: 'PlayListDetail',
        component: () => import('views/playlists/PlayListDetail'),
      },
      {
        path: '/toplists',
        name: 'TopLists',
        component: () => import('views/toplists/TopLists'),
      },
      {
        path: '/artists',
        name: 'Artists',
        component: () => import('views/artist/Artists'),
      },
      {
        path: '/artist',
        component: () => import('views/artist/ArtistDetail'),
        children: [
          {
            path: '',
            name: 'ArtistSong',
            component: () => import('views/artist/child/ArtistSong'),
          },
          {
            path: 'album',
            name: 'ArtistAlbum',
            component: () => import('views/artist/child/ArtistAlbum'),
          },
          {
            path: 'mv',
            name: 'ArtistMv',
            component: () => import('views/artist/child/ArtistMv'),
          },
          {
            path: 'desc',
            name: 'ArtistDesc',
            component: () => import('views/artist/child/ArtistDesc'),
          },
        ],
      },
      {
        path: '/song',
        name: 'Song',
        component: () => import('views/song/Song'),
      },
      {
        path: '/mvs',
        name: 'Mvs',
        component: () => import('views/mymv/Mvs.vue'),
      },
      {
        path: '/mv',
        name: 'MyMv',
        component: () => import('views/mymv/MyMv.vue'),
      },
      {
        path: '/video',
        name: 'MyVideo',
        component: () => import('views/myvideo/MyVideo'),
      },
      {
        path: '/albums',
        name: 'Album',
        component: () => import('views/album/Album'),
      },
      {
        path: '/album',
        name: 'AlbumDetail',
        component: () => import('views/album/AlbumDetail'),
      },
      {
        path: '/user',
        name: 'UserDetail',
        component: () => import('views/user/UserDetail'),
      },
      {
        path: '/user/event',
        name: 'UserEvent',
        component: () => import('views/user/UserEvent'),
      },
      {
        path: '/user/follows',
        name: 'UserFollows',
        component: () => import('views/user/UserFollows'),
      },
      {
        path: '/user/fans',
        name: 'UserFans',
        component: () => import('views/user/UserFans'),
      },
      {
        path: '/search',
        name: 'Search',
        component: () => import('views/search/Search'),
        children: [
          {
            path: '/search',
            name: 'SearchSong',
            component: () => import('views/search/child/SearchSong'),
          },
          {
            path: '/search',
            name: 'SearchArtist',
            component: () => import('views/search/child/SearchArtist'),
          },
          {
            path: '/search',
            name: 'SearchAlbum',
            component: () => import('views/search/child/SearchAlbum'),
          },
          {
            path: '/search',
            name: 'SearchPlaylist',
            component: () => import('views/search/child/SearchPlaylist'),
          },
          {
            path: '/search',
            name: 'SearchVideo',
            component: () => import('views/search/child/SearchVideo'),
          },
          {
            path: '/search',
            name: 'SearchLyric',
            component: () => import('views/search/child/SearchLyric'),
          },
          {
            path: '/search',
            name: 'SearchRadio',
            component: () => import('views/search/child/SearchRadio'),
          },
          {
            path: '/search',
            name: 'SearchUser',
            component: () => import('views/search/child/SearchUser'),
          },
          {
            path: '/search',
            name: 'SearchNotFound',
            component: () => import('views/search/child/SearchNotFound'),
          },
        ],
      },
      {
        path: '/my',
        name: 'My',
        component: () => import('views/my/My'),
        children: [
          {
            path: 'playlist',
            name: 'MyPlaylist',
            component: () => import('views/my/child/MyPlaylist'),
          },
          {
            path: 'artists',
            name: 'MyArtists',
            component: () => import('views/my/child/MyArtists'),
          },
          {
            path: 'follows',
            name: 'Follows',
            component: () => import('views/my/child/Follows'),
          },
          {
            path: 'fans',
            name: 'Fans',
            component: () => import('views/my/child/Fans'),
          },
          {
            path: 'videos',
            name: 'Videos',
            component: () => import('views/my/child/Videos'),
          },
          {
            path: 'edit',
            name: 'MyEdit',
            component: () => import('views/my/child/MyEdit'),
          },
        ],
      },
      {
        path: '/friend',
        name: 'Friend',
        component: () => import('views/friend/Friend'),
      },
    ],
  },
  {
    name: '404',
    path: '/404.html',
    component: () => import('views/notfound/NotFound'),
  },
  {
    path: '*',
    redirect: {
      name: '404',
    },
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

export default router;
